package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.produce_team.ProduceTeamOrderInformationAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartFeedbackResponse;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderWorkPartScheduleEditView;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IWorkPartDetailView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderWorkPartScheduleEditPresenter;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.WorkPartDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.List;

public class ProduceWorkpartDetailActivity extends BaseActivity implements IProductOrderWorkPartScheduleEditView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_workpart_name, tv_product_name, edt_specifition_size, edt_process_num, edt_process_cycle, edt_receive_people, edt_receive_time;
    private ListViewForScrollView lv_produce_plan;
    private Button btn_modify_plan, btn_pause;

    private ProduceTeamOrderInformationAdapter adapter;

    private WorkPartOrderBean workPartOrderBean;
    private WorkPartFeedbackResponse workPartFeedbackResponse;
    private WorkPartBean workPartBean;

    private ProductOrderWorkPartScheduleEditPresenter productOrderWorkPartScheduleEditPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_workpart_detail);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_workpart_name = findViewById(R.id.tv_workpart_name);
        tv_product_name = findViewById(R.id.tv_product_name);
        edt_specifition_size = findViewById(R.id.edt_specifition_size);
        edt_process_num = findViewById(R.id.edt_process_num);
        edt_process_cycle = findViewById(R.id.edt_process_cycle);
        edt_receive_people = findViewById(R.id.edt_receive_people);
        edt_receive_time = findViewById(R.id.edt_receive_time);
        lv_produce_plan = findViewById(R.id.lv_produce_plan);
        btn_modify_plan = findViewById(R.id.btn_modify_plan);
        btn_pause = findViewById(R.id.btn_pause);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.order_information));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            workPartFeedbackResponse = (WorkPartFeedbackResponse) intent.getSerializableExtra(ConstantUtil.KEY_2);
        }

        workPartOrderBean = workPartBean.getWorkPartOrderById(workPartFeedbackResponse.getWorkPartOrderId());

        tv_workpart_name.setText(workPartBean.getWorkPartName());
        tv_product_name.setText(" / " + workPartBean.getProductName());
        edt_process_cycle.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate()) + " ~ " + DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()));

        CommonEnum.ProductShapeType shape = workPartBean.getShapeType().getType();
        if (null != shape) {
            if (shape == CommonEnum.ProductShapeType.ROUND) {
                edt_specifition_size.setText("直径" + workPartBean.getShapeType().getDiameter() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                edt_specifition_size.setText("长度" + workPartBean.getShapeType().getWidth() + "mm*" + "高度" + workPartBean.getShapeType().getHeight() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                edt_specifition_size.setText(workPartBean.getShapeType().getDesc());
            }
        }

        if (workPartOrderBean != null) {
            edt_process_num.setText(workPartOrderBean.getAmount() + workPartBean.getUnit());
            edt_receive_people.setText(workPartOrderBean.getAssignReceiveAccountName());
            edt_receive_time.setText(DateUtil.getYyyy_Mm_Dd(workPartOrderBean.getReceiveAssignOrderTime()));
        }

        adapter = new ProduceTeamOrderInformationAdapter(this);
        adapter.setStrUnit(workPartBean.getUnit());
        adapter.setData(workPartBean.getWorkPartScheduleList());
        lv_produce_plan.setAdapter(adapter);

        productOrderWorkPartScheduleEditPresenter = new ProductOrderWorkPartScheduleEditPresenter(this);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_modify_plan.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 修改计划
            case R.id.btn_modify_plan:
                productOrderWorkPartScheduleEditPresenter.productOrderWorkPartScheduleEdit();
                break;
            // 暂停
            case R.id.btn_pause:
                break;
        }
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public List<WorkPartScheduleBean> getList() {
        return workPartBean.getWorkPartScheduleList();
    }

    @Override
    public void getSuccessed() {
        showToast(getResources().getString(R.string.modify_plan_success));
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_2, mIntent);
        finish();
    }

    @Override
    public void getFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
